/**
 * 
 */
package net.microlinktech.testing.ui.table;

import java.awt.Component;
import java.awt.Container;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.table.TableColumn;

import net.microlinktech.testing.ui.dialog.MessageDialog;

/**
 * @author moxin
 * 
 */
public final class TableConfig {

    private TableConfig() {

    }

    /**
     * 隐藏表格中的某一列
     * 
     * @param table
     *            表格
     * @param index
     *            要隐藏的列 的索引
     */
    public static void hideColumn(JTable table, int... indexs) {
        for (int index : indexs) {
            TableColumn tc = table.getColumnModel().getColumn(index);
            tc.setMaxWidth(0);
            tc.setPreferredWidth(0);
            tc.setMinWidth(0);
            tc.setWidth(0);

            table.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(0);
            table.getTableHeader().getColumnModel().getColumn(index).setMinWidth(0);
        }
    }

    public static void scrollRectToVisible(Component component, Rectangle aRect) {
        Container parent;
        int dx = component.getX(), dy = component.getY();

        for (parent = component.getParent(); parent != null && (!(parent instanceof JViewport)); parent = parent
                .getParent()) {
            Rectangle bounds = parent.getBounds();

            dx += bounds.x;
            dy += bounds.y;
        }

        if (parent != null) {
            aRect.x += dx;
            aRect.y += dy;

            ((JComponent) parent).scrollRectToVisible(aRect);
            aRect.x -= dx;
            aRect.y -= dy;
        }
    }

    public static void setSelectedRow(JTable table, JScrollPane scrollPane, int modelRow) {
        int viewRow = table.convertRowIndexToView(modelRow);
        table.setRowSelectionInterval(viewRow, viewRow);
        Rectangle rect = table.getCellRect(viewRow, 0, true);
        int halfTableHeight = scrollPane.getHeight() / 2;
        if (rect.y - halfTableHeight > 0) {
            rect.y = rect.y - halfTableHeight + 30;
            rect.height += scrollPane.getHeight();
        }
        table.scrollRectToVisible(rect); // 设置滚动条值
    }

    public static boolean isSelected(JTable table) {
        boolean isSelected = table.getSelectedRow() >= 0;
        if (!isSelected) {
            MessageDialog.showInformation("请先在表格中选择一行");
        }
        return isSelected;
    }

    /**
     * 清空表格的选中状态
     * 
     * @param table
     */
    public static void clearTableSelection(JTable table) {
        table.getSelectionModel().clearSelection();
        if (table.getCellEditor() != null) {
            table.getCellEditor().stopCellEditing();
        }
    }

    /**
     * 获取视图选择行对应模型中的行
     * 
     * @param table
     * @return
     */
    public static int convertRowIndexToModel(JTable table) {
        return table.convertRowIndexToModel(table.getSelectedRow());
    }

}
